<?php

namespace app\api\controller;


use app\admin\model\jeek\shop\SkuGoods;
use app\common\controller\Api;

use app\common\model\Couponlog;
use app\common\model\Order;
use app\common\model\User;
use fast\Date;
use tencent\api\Im;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\Request;

/**
 * 定时任务
 */
class Task extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function task()
    {
        # 图文剩余几分钟 给提醒
        $this->orderText();
        # 自动结束订单
        $this->orderEnd();
        //优惠卷到期执行
        $this->couponLog();
        //订单关闭执行
        $this->orderClose();
    }


    /**
     * Account:处理图文订单
     * User: Ixiangang
     * Date: 2025/3/7
     * Time: 15:47
     */
    public function orderText()
    {
        $order = new Order();
        $order->where('status', '=', 'active')
            ->where('meter_is', '=', '1')
            ->where('already_notified', 0)
            ->chunk(50, function ($list) use ($order) {
                foreach ($list as $v) {
                    $currentTime = time();
                    $endTime = $v->end_time;
                    $timeDifference = $endTime - $currentTime;
                    $com = "未知错误";
                    if ($v->order_type == "text") {
                        $com = "【图文聊天】";
                    }
                    if ($v->order_type == "audio") {
                        $com = "【电话聊天】";
                    }
                    if ($v->order_type == "video") {
                        $com = "【视频聊天】";
                    }
                    // 如果 end_time 在当前时间的 分钟内
                    if ($timeDifference > 0 && $timeDifference <= 120) {    // 180 秒 = 3 分钟
                        // 提醒用户逻辑
                        // 例如：发送通知、记录日志等
                        // 你可以在这里调用你的提醒逻辑
                        $user_tencent_account = User::where('id', $v->user_id)->value('tencent_account');
                        $goods_tencent_account = SkuGoods::where('id', $v->goods_id)->value('tencent_account');
                        $im = new Im();
                        $com = "系统温馨提示：我们的" . $com . "咨询时间还剩余2分钟，请您把握好时间和话题哦！";
                        $im->from_to($goods_tencent_account, $user_tencent_account, $com);
                        $v->already_notified = 1;
                        $v->save();
                    }
                }
            });
        echo "咨询师给用户发自定义消息";
        echo '<pre>';
    }

    /**
     * Account:聊天时间到结束订单
     * User: Ixiangang
     * Date: 2025/2/10
     * Time: 15:03
     */
    public function orderEnd()
    {
        $order = new Order();
        $order->where('status', '=', 'active')
            ->where('meter_is', '=', '1')
            ->chunk(50, function ($list) use ($order) {
                foreach ($list as $v) {
                    if ($v->end_time < time()) {
                        # 关闭会话更改订单状态
                        $order->orderEndChatSessions($v->id);
                    }
                }
            });
        echo "聊天时间到--结束订单";
        echo '<pre>';
    }


    /**
     * Account:过期优惠卷执行
     * User: Ixiangang
     * Date: 2025/2/8
     * Time: 15:12
     */
    public function couponLog()
    {
        $couponlog = new Couponlog();
        $couponlog->where(['use_for' => '1', 'expiration' => ['<', time()]])
            ->chunk(50, function ($list) use ($couponlog) {
                foreach ($list as $v) {
                    $couponlog->where($couponlog->getPk(), $v->id)->update(['use_for' => '3']);
                }
            });
        echo '过期优惠卷';
        echo '<pre>';
    }

    /**
     * Account:关闭订单
     * User: Ixiangang
     * Date: 2025/2/8
     * Time: 15:13
     */
    public function orderClose()
    {
        $order = new Order();
        $order_close_time = 30; //30分钟未支付订单自动取消
        $order->where('status', '=', 'paytreat')
            ->where('createtime', '<', Date::unixtime('minute', '-' . $order_close_time))
            ->chunk(50, function ($list) use ($order) {
                foreach ($list as $v) {
                    $order->orderClose($v->id);
                }
            });
        echo '订单关闭';
        echo '<pre>';
    }
}